
using System;

namespace stefc.geo.lib
{
	/// <summary>
	/// Cartesian coordinate
	/// </summary>
	public class Cartesian 
	{
		private readonly double x;
		private readonly double y;
		
		private static volatile Cartesian zero;
		
		public Cartesian(double x, double y)
		{
			this.x=x;
			this.y=y;
		}
			
		public double X { get { return this.x; } }
			
		public double Y { get { return this.y; } }

		public double GetDistance(Cartesian to)
		{
			if(to != null)
			{
				double dx=Math.Abs(to.X-this.X);
				double dy=Math.Abs(to.Y-this.Y);
				
				return Math.Sqrt(dx*dx+dy*dy);
			}
			
			return 0.0;
		}
		
		public override string ToString ()
		{
			return string.Format("[Cartesian: X={0}, Y={1}]", X, Y);
		}
		
		public static Cartesian Zero 
		{
			get 
			{
				if(zero==null)
					zero=new Cartesian(0,0);
				return zero;
			}
		}
	}
}
